import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'

export const Layout = () => import('@/Layout/index.vue')
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path:'/login',
      component:()=>import('../views/Login/index.vue')
    },
    {
      path:'/',
      component: Layout,
      redirect:'/new',
      children:[
        {
          path:'new',
          name:'new',
          component:()=>import('@/views/new/index.vue')
        },
        {
          path:'home',
          name:'home',
          component:()=>import('@/views/home/index.vue')
        },
      ]
    },
    {
      path:'/',
      name:'Layout',
      component:Layout,
      children:[
        {
          path:'room',
          name:'room',
          component:()=>import('@/views/room/index.vue')
        },
        
      ]
    }
  
  ]
})


const WHITE_LIST = ['/login']
router.beforeEach((to,from,next)=> {
  const store = useUserStore()

  if (store?.token) {
   next()
  } else {
    if (WHITE_LIST.includes(to.path)) {
      next()

    } else {
      next('/login')
    }
 }
})

export default router
